import { useInitStore }  from '@/common/hox/initStore'
import { TicketModeEnum } from '@/utils/enum'
import request from '@/utils/request'
import { useRequest } from 'ahooks'
import { createGlobalStore } from 'hox'
import appConfig from '@/utils/appConfig'


/**
 * @description 获取应用配置数据
 */
async function getAppSetting(): Promise<{
  appTemplate: number;
  ticketMode: string;
  cashierScene: string;
  banner: any;
}> {
  return request.get("/api/Common/GetAppSetting", {params: {'AppConfigId': appConfig.appConfigId, 'SmallAppId': appConfig.smallAppId,'MerchantId': appConfig.merchantId}})
}
export const [useAppSettingStore, getAppSettingStore] = createGlobalStore(() => {
  const { initReady } = useInitStore()
  const getAppSettingApi = useRequest(getAppSetting, {
    ready: initReady,
  })
  return {
    appTemplate: getAppSettingApi.data?.appTemplate,
    ticketMode: (getAppSettingApi.data?.ticketMode || '').split(",").filter(item => item).map(item => parseInt(item)) as TicketModeEnum[],
    // ticketMode: [0, 1] as TicketModeEnum[],
    banner: getAppSettingApi.data?.banner,
    cashierScene: JSON.parse(getAppSettingApi.data?.cashierScene || '[]') as {
      client: '',
      jumpUrl: string,
    }[]
  }
})